package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.DestVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.DestQueryObject;
import cn.wolfcode.luowowo.search.query.IndexSearchQuery;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 猫教授
 * @date 2019/8/9 15:36
 */
@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelCommendService travelCommendService;

    /**
     * 目的地页面的初始访问方法
     *
     * @param qo
     * @param model
     * @return
     */

    @RequestMapping("")
    public String list(HttpServletRequest request, @UserParam UserInfo userInfo, @ModelAttribute("qo") DestinationQuery qo, Model model) {
        //先查询热门的地区
        List<Region> hotRegions =regionService.selectHotRegions(new RegionQuery());
        model.addAttribute("hotRegions",hotRegions);

        //查询所有国家，并按照首字母排序
        List<DestVO> destVOS = destinationService.selectCountry();
        model.addAttribute("destlist",destVOS);


        //获取所有的月份
        List<DestinationlThemeDetial> destinationThemeDetials = destinationService.selectMonth();
        model.addAttribute("months",destinationThemeDetials);

        //获取所有的主题
        List<DestinationTheme> destinationThemes = destinationService.selectTheme();
        model.addAttribute("destinationThemes",destinationThemes);


        List<StrategyStatisVO> scs=strategyStatisRedisService.selectSort(IStrategyStatisRedisService.HOTS);
        //scs 攻略推荐
        model.addAttribute("scs", scs.subList(0, 1));
        request.getSession().setAttribute("userInfo", userInfo);
        //tcs 游记的推荐
        model.addAttribute("tcs", travelCommendService.queryCommendTop5());


        return "destination/index";

    }

    /**
     * 目的地模块首页的显示
     * @param regionId
     * @param model
     * @return
     */
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        List<Destination> hotdestTpl=null;
        if(regionId == -1L){
            hotdestTpl=destinationService.selectHotdestTplByChinaId();
        }else {
            //先查地区拿到数组再查目的地
            Region region = regionService.selectByPrimaryKey(regionId);
            Long[] refIds = region.getRefIds();
            hotdestTpl=destinationService.selectHotdestTplByParentId(refIds);
        }
        int size = hotdestTpl.size();
        //左边列表
        model.addAttribute("leftDests", hotdestTpl.subList(0,size/2+1));
        //右边列表
        model.addAttribute("rightDests", hotdestTpl.subList(size/2+1,size));
        return "destination/hotdestTpl";

    }

    /**
     * 目的地详情的吐丝
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("guide")
    public String guide(Long id,Model model) {
        ArrayList<Destination> toasts=new ArrayList<>();
        getToasts(id,toasts);
        Collections.reverse(toasts);
        toasts.remove(toasts.size()-1);
        model.addAttribute("toasts",toasts );
        //最后一个特殊处理
        Destination dest = destinationService.selecOne(id);
        model.addAttribute("dest",dest );

        //根据目的地ID查询那个分类列表
        List<StrategyCatalog> catalogs=strategyCatalogService.selectCatalogsByDestId(id);
        model.addAttribute("catalogs", catalogs);
        //根据目的地Id查询点击量最高的3篇帖子,分别需要显示id ,title,封面,然后再来个排序
        List<StrategyDetail> strategyDetails=strategyDetailService.selectDetailsByDestIdNumTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);

        //根据当前目的地查询出附近的目的地
        Destination destination = destinationService.selectByPrimaryKey(id);
        if(destination!=null){
        List<Destination> destinations = destinationService.selectTude(destination.getLongitude(), destination.getLatitude());
        model.addAttribute("destinations",destinations);
        }
        return "destination/guide";
    }
    /**
     * 内部方法用来迭代吐丝的
     * @param destinationId
     * @param toasts
     */
    private void getToasts(Long destinationId,ArrayList toasts){
        Destination destination = destinationService.selecOne(destinationId);
        toasts.add(destination);
        if (destination.getParent()!=null){
            getToasts(destination.getParent().getId(),toasts);
        }
    }

    @RequestMapping("surveyPage")
    public String surveyPage(@ModelAttribute("qo") StrategyDetailQuery qo,Model model) {
        ArrayList<Destination> toasts=new ArrayList<>();
        getToasts(qo.getDestId(),toasts);
        Collections.reverse(toasts);
        toasts.remove(toasts.size()-1);
        model.addAttribute("toasts",toasts );
        Destination dest = destinationService.selecOne(qo.getDestId());
        model.addAttribute("dest",dest );
        return "destination/survey";
    }
    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo") StrategyDetailQuery qo,Model model) {
        //根据目的地ID查询那个分类列表
        List<StrategyCatalog> catalogs=strategyCatalogService.selectCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog );
        //查询第一篇文章
        StrategyDetail detail=strategyDetailService.get(catalog.getDetails().get(0).getId());
        StrategyContent content = strategyDetailService.getContent(detail.getId());
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);

        return "destination/surveyTpl";
    }
    @RequestMapping("travels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Model model) {
        model.addAttribute("pageInfo",travelService.selectForList(qo));

        return "destination/travelTpl";
    }

    @RequestMapping("getDestByMonth")
    public String getDestByMonth(Model model,@ModelAttribute("month")Long month){
        List<Destination> destByMonth = destinationService.getDestByMonth(month);
        model.addAttribute("destByMonth",destByMonth);
        return  "destination/monthdestTpl";
    }
    @RequestMapping("getThemes")
    public String getThemes(Model model,@ModelAttribute("themeId")Long themeId){
        List<DestinationlThemeDetial> detailByTheme = destinationService.getDetailByTheme(themeId);
        model.addAttribute("destThemes",detailByTheme);
        return  "destination/destThemeTpl";
    }

    @RequestMapping("destByTheme")
    public String destByTheme(Model model,@ModelAttribute("qo")DestQueryObject qo){
        //获取所有的月份
        List<DestinationlThemeDetial> destinationThemeDetials = destinationService.selectMonth();
        model.addAttribute("months",destinationThemeDetials);
        //获取所有的节日
        List<DestinationlThemeDetial> destinationFestival = destinationService.selectFestival();
        model.addAttribute("festival",destinationFestival);

        //获取所有的主题及其明细
        List<DestinationTheme> destinationThemes = destinationService.selectTheme();
        DestVO themesVO = destinationService.selectThemeAndDetial("节日");
        model.addAttribute("destlist",themesVO);

        //获取出行天数
        List<DestinationlThemeDetial> days = destinationService.selectDay();
        model.addAttribute("days",days);

        return "destination/destFilter";

    }

    @RequestMapping("form")
    public String form(Model model,@ModelAttribute("qo")DestQueryObject qo){
        Page<DestinationTemplate> templates = destinationSearchService.selectByTheme(qo);
        List<DestinationTemplate> destinationTemplates = templates.getContent();
        model.addAttribute("page",templates);
        return "destination/deatPageTpl";
    }

    @RequestMapping("q")
    public String q(Model model, @ModelAttribute("qo") IndexSearchQuery qo) {
        //先处理0号目的地查询目的地
            SearchResultVO searchResultVO = new SearchResultVO();
            AggregatedPage<DestinationTemplate> dests = destinationSearchService.selectByKeyword(qo, "name", "info");
            searchResultVO.setDests(dests.getContent());
            model.addAttribute("data", searchResultVO);
            return "index/searchAll";
        }
    @RequestMapping("spot")
    public String spot(Model model, Long destId) {
        List<Attractions> spots = destinationService.selectSpot(destId);
        List<StrategyDetail> details = strategyDetailService.selectDetailsByDestIdNumTop3(destId);
        model.addAttribute("details", details);
        model.addAttribute("spots", spots);
            return "destination/destspotTpl";
        }

}
